package org.phalanx.server.sql;

import java.util.ArrayList;

public class SQLAction {
	private String actionType;
	private String tableName;
	private ArrayList<String> fields = new ArrayList<String>();
	private ArrayList<String> criteriaAND = new ArrayList<String>();
	private ArrayList<String> criteriaOR = new ArrayList<String>();
	private ArrayList<String> values = new ArrayList<String>();
	
	public static String SELECT = "SELECT";
	public static String INSERT = "INSERT";
	public static String UPDATE = "UPDATE";
	public static String DELETE = "DELETE";
	public static String EQUALS = "=";
	public static String NOTEQUALS = "!=";
	public static String GREATERTHAN = ">";
	public static String LESSTHAN = "<";
	
	
	public SQLAction(String type, String tableName){
		actionType = type;
		this.tableName = tableName;
	}
	
	public void addField(String fieldName){
		fields.add(fieldName);
	}
	
	public void addValues(String valueName){		
		values.add(valueName);
	}
	
	public void addSearchCriteriaAND(String fieldName, String operator, String value){
		criteriaAND.add(fieldName + " " + operator + " '" + value + "'");
	}
	
	public void addSearchCriteriaOR(String fieldName, String operator, String value){
		criteriaOR.add(fieldName + " " + operator + " '" + value + "'");
	}
	
	public String getActionString(){
		String actionString = new String();
	
		if (actionType.equals(SELECT) || actionType.equals(DELETE))
			actionString = getSelectActionString();
		else if (actionType.equals(INSERT))
			actionString = getInsertActionString();
		else if (actionType.equals(UPDATE))
			actionString = getUpdateActionString();
		
		return actionString;
	}
	
	private String getSelectActionString(){
		String selectActionString = new String();
		
		if (!fields.isEmpty()){
			selectActionString += actionType + " ";
			
			for (int i = 0; i < fields.size(); i++){
				if (i != 0)
					selectActionString += ", ";
				selectActionString += fields.get(i);
			}
			
			selectActionString += " FROM " + tableName + " ";
			String criteriaString1 = getANDCriteria();
			String criteriaString2 = getORCriteria();
			
			if (!criteriaString1.equals(new String()) || !criteriaString2.equals(new String())){
				selectActionString += " WHERE ";
				selectActionString += criteriaString1 + " " + criteriaString2;
			}
			
			
		}	
		return selectActionString;
	}
	
	private String getANDCriteria(){
		String andCriteriaString = new String();
		
		if (!criteriaAND.isEmpty()){
			for (int i = 0; i < criteriaAND.size(); i++){
				if (i != 0)
					andCriteriaString += " AND ";
				andCriteriaString += criteriaAND.get(i);
			}
		}
		
		return andCriteriaString;
	}
	
	private String getORCriteria(){
		String andCriteriaString = new String();

		if (!criteriaOR.isEmpty()){
			if (criteriaOR.size() == 1){
				andCriteriaString += " OR " + criteriaOR.get(0);
			}
			else {
				for (int i = 0; i < criteriaOR.size(); i++){
					if (i != 0)
						andCriteriaString += " OR ";
					andCriteriaString += criteriaOR.get(i);
				}
			}
		}
		
		return andCriteriaString;
	}
	
	private String getUpdateActionString(){
		String updateActionLine = new String();
		
		if (!fields.isEmpty() && !values.isEmpty()){
			updateActionLine += actionType + " " + tableName + " SET ";
			
			for (int i = 0; i < fields.size(); i++){
				if (i != 0)
					updateActionLine += ", ";
				updateActionLine += fields.get(i) + " = " + sanitizeValue(values.get(i));
			}
			
			String criteriaString1 = getANDCriteria();
			String criteriaString2 = getORCriteria();
			
			if (!criteriaString1.equals(new String()) || !criteriaString2.equals(new String())){
				updateActionLine += " WHERE ";
				updateActionLine += criteriaString1 + " " + criteriaString2;
			}
		}
		
		return updateActionLine;
	}
	
	private String sanitizeValue(String value){
		String sanitizedValue = "";
		
		if (value == null){
			sanitizedValue = "NULL";
		} else {
			sanitizedValue = "'" + value + "'";
		}
		
		return sanitizedValue;
	}
	
	private String getInsertActionString(){
		String writeSQLString = new String();
		
		if (!fields.isEmpty() && !values.isEmpty()){
			writeSQLString += actionType + " INTO " + tableName + " ";
			writeSQLString += getFieldsForInsert();
			writeSQLString += getValuesForInsert();
		}
		
		return writeSQLString;
	}
	
	private String getFieldsForInsert(){
		String fieldListString = new String();
		
		fieldListString += "(";
		
		for (int i = 0; i < fields.size(); i++){
			if (i != 0)
				fieldListString += ", ";
			fieldListString += fields.get(i);
		}
		
		fieldListString += ") ";
		
		return fieldListString;
	}
	
	private String getValuesForInsert(){
		String valueListString = new String();
		
		valueListString += "VALUES (";
		
		for (int i = 0; i < values.size(); i++){
			if (i != 0)
				valueListString += ", ";
			valueListString += sanitizeValue(values.get(i));
		}
		
		valueListString += ") ";
		
		return valueListString;
	}
	
	
	
	public static void main(String[] args){
		SQLAction a = new SQLAction(UPDATE, "user");
		a.addField("email");
		a.addValues("sday00@bluefishtech.net");
		a.addSearchCriteriaAND("userid", EQUALS, "1");
		System.out.println(a.getActionString());
	}
}
